﻿using System;
using System.Web.Security;
using System.Web.Services;
using Dost.Khcn.Web.Common;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Biz;

namespace Dost.Khcn.Web.Employees
{
    public partial class List : System.Web.UI.Page
    {
        #region Properties

        private static EmployeeBiz _employeeBiz;
        private static EmployeeBiz EmployeeManager
        {
            get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebCommon.CurrrentEmployee == null)
            {
                FormsAuthentication.RedirectToLoginPage();
                return;
            }

            if (!WebCommon.CurrrentEmployee.IsAdmin)
            {
                Response.Redirect("~/NoPermission.aspx");
            }
        }

        #region Web methods

        [WebMethod(EnableSession = true)]
        public static object EmployeeList(int jtStartIndex, int jtPageSize, string jtSorting, string name, int roleId)
        {
            return EmployeeManager.GetAllEmployeesWs(jtStartIndex, jtPageSize, jtSorting, name, roleId);
        }

        [WebMethod(EnableSession = true)]
        public static object CreateEmployee(Employee record)
        {
            return EmployeeManager.CreateEmployeeWs(record);
        }

        [WebMethod(EnableSession = true)]
        public static object UpdateEmployee(Employee record)
        {
            return EmployeeManager.UpdateEmployeeWs(record);
        }

        [WebMethod(EnableSession = true)]
        public static object DeleteEmployee(int Id)
        {
            if (WebCommon.CurrrentEmployee != null && WebCommon.CurrrentEmployee.IsAdmin && WebCommon.CurrrentEmployee.Id != Id)
            {
                return EmployeeManager.DeleteEmployeeWs(Id);
            }
            return new { Result = "ERROR", Message = "Error" };
        }

        #endregion
    }
}